package com.tiger.project.service.impl;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.tiger.common.core.redis.RedisCache;
import com.tiger.common.utils.Code;
import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.StringUtils;
import com.tiger.project.domain.CommonDecision;
import com.tiger.project.domain.CommonDecisionEnclosure;
import com.tiger.project.mapper.CommonDecisionEnclosureMapper;
import com.tiger.project.mapper.CommonDecisionMapper;
import com.tiger.project.service.ICommonDecisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【决策依据】Service业务层处理
 *
 * @author standard
 * @date 2022-09-14
 */
@Service
public class CommonDecisionServiceImpl implements ICommonDecisionService {
    @Autowired
    private CommonDecisionMapper commonDecisionMapper;
    @Autowired
    private CommonDecisionEnclosureMapper enclosureMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询【决策依据】
     *
     * @param id 【决策依据】主键
     * @return 【决策依据】
     */
    @Override
    public CommonDecision selectCommonDecisionById(Long id) {
        CommonDecision commonDecision = commonDecisionMapper.selectCommonDecisionById(id);
        //附件
        CommonDecisionEnclosure decisionEnclosure = new CommonDecisionEnclosure();
        decisionEnclosure.setDecisionId(id);
        List<CommonDecisionEnclosure> commonDecisionEnclosures = enclosureMapper.selectCommonDecisionEnclosureList(decisionEnclosure);
        if (commonDecisionEnclosures != null && commonDecisionEnclosures.size() > 0){
            commonDecision.getDecisionEnclosureList().addAll(commonDecisionEnclosures);
        }
        return commonDecision;
    }

    /**
     * 查询【决策依据】列表
     *
     * @param commonDecision 【决策依据】
     * @return 【决策依据】
     */
    @Override
    public List<CommonDecision> selectCommonDecisionList(CommonDecision commonDecision) {
        return commonDecisionMapper.selectCommonDecisionList(commonDecision);
    }

    /**
     * 新增【决策依据】
     *
     * @param commonDecision 【决策依据】
     * @return 结果
     */
    @Override
    public int insertCommonDecision(CommonDecision commonDecision) {

        int code = redisCache.getCacheObjectCode(LocalDate.now().format(Code.fmt) + "mb");
        addCode(code);
        commonDecision.setCode(String.format("%s%s%04d", "MB", LocalDate.now().format(Code.fmt),code));
        commonDecision.setCreatedAt(DateUtils.getNowDate());
        commonDecision.setIsDeleted(0);
        int i = commonDecisionMapper.insertCommonDecision(commonDecision);
        change(commonDecision);
        return i;
    }

    /**
     * 修改【决策依据】
     *
     * @param commonDecision 【决策依据】
     * @return 结果
     */
    @Override
    public int updateCommonDecision(CommonDecision commonDecision) {
        change(commonDecision);
        commonDecision.setUpdateAt(DateUtils.getNowDate());
        return commonDecisionMapper.updateCommonDecision(commonDecision);
    }

    /**
     * 批量删除【决策依据】
     *
     * @param ids 需要删除的【决策依据】主键
     * @return 结果
     */
    @Override
    public int deleteCommonDecisionByIds(Long[] ids) {
        return commonDecisionMapper.deleteCommonDecisionByIds(ids);
    }

    /**
     * 删除【决策依据】信息
     *
     * @param id 【决策依据】主键
     * @return 结果
     */
    @Override
    public int deleteCommonDecisionById(Long id) {
        return commonDecisionMapper.deleteCommonDecisionById(id);
    }


    private void change(CommonDecision commonDecision){
        //附件
        enclosureMapper.deleteCommonDecisionEnclosureByDecisionId(commonDecision.getId());
        if (StringUtils.isNotEmpty(commonDecision.getDecisionEnclosureList())){
            commonDecision.getDecisionEnclosureList().forEach(item ->{
                item.setCreatedAt(DateUtils.getNowDate());
                item.setDecisionId(commonDecision.getId());
                item.setState("1");//上传成功
                enclosureMapper.insertCommonDecisionEnclosure(item);
            });
        }
    }


    /**
     * 生成编码
     */
    private synchronized void addCode(int code) {
        redisCache.setCacheObject(LocalDate.now().format(Code.fmt) + "mb", code + 1);
    }

}
